package gui;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.UIManager;

import utils.Operations;

public class DatabaseConnectionWizard extends javax.swing.JPanel
{
	private static final long serialVersionUID = 1L;

	private static final String PREFERRED_LOOK_AND_FEEL = UIManager.getSystemLookAndFeelClassName();
	
	private JButton startButton;
	private JButton quickSaveButton; 
	private JButton quickLoadButton;
	private JButton clearButton;
	private JButton exitButton;
	private JToolBar toolbar;

	private JComboBox dbType;
	private JLabel dbType_lbl;
	private JTextField dbHost;
	private JLabel dbHost_lbl;
	private JTextField dbUserName;
	private JLabel dbUserName_lbl;
	private JPasswordField dbPassword;
	private JLabel dbPassword_lbl;
	private JTextField dbName;
	private JLabel dbName_lbl;

	private Properties params = new Properties();
	
	public DatabaseConnectionWizard() {
		setLookandFeel();
		initComponents();
	}

	private void setLookandFeel() {
		try {
			UIManager.setLookAndFeel(PREFERRED_LOOK_AND_FEEL);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private void initComponents() {
		setBackground(Color.white);
		setForeground(Color.black);
		setEnabled(false);
		setLayout(null);

		add(getToolbar());

		add(getDbType_lbl());
		add(getDbType());
		add(getDbHost_lbl());
		add(getDbHost());
		add(getDbUserName_lbl());
		add(getDbUserName());
		add(getDbPassword_lbl());
		add(getDbPassword());
		add(getDbName_lbl());
		add(getDbName());
	}

	private JLabel getDbType_lbl() {
		if (dbType_lbl == null) {
			dbType_lbl = new JLabel();
			dbType_lbl.setText("DB Type:");
			dbType_lbl.setBounds(20, 70, 70, 20);
		}
		return dbType_lbl;
	}

	private JComboBox getDbType() {
		if (dbType == null) {  
			Object[] types = { "", "MySQL", "SQL Server", "PostgreSQL" };
			
			dbType = new JComboBox();
			dbType.setModel(new DefaultComboBoxModel(types));
			dbType.setSelectedIndex(0);
			dbType.setToolTipText("Choose the Database type.");
			dbType.setBounds(100, 70, 100, 20);
		}
		return dbType;
	}

	private JLabel getDbHost_lbl() {
		if (dbHost_lbl == null) {
			dbHost_lbl = new JLabel();
			dbHost_lbl.setText("DB Host:");
			dbHost_lbl.setBounds(20, 100, 70, 20);
		}
		return dbHost_lbl;
	}

	private JTextField getDbHost() {
		if (dbHost == null) {
			dbHost = new JTextField();
			dbHost.setToolTipText("Enter the Database host server.");
			dbHost.setBounds(100, 100, 140, 20);
		}
		return dbHost;
	}

	private JLabel getDbName_lbl() {
		if (dbName_lbl == null) {
			dbName_lbl = new JLabel();
			dbName_lbl.setText("Database:");
			dbName_lbl.setBounds(20, 130, 70, 20);
		}
		return dbName_lbl;
	}

	private JTextField getDbName() {
		if (dbName == null) {
			dbName = new JTextField();
			dbName.setToolTipText("Enter the Database name.");
			dbName.setBounds(100, 130, 140, 20);
		}
		return dbName;
	}

	private JLabel getDbUserName_lbl() {
		if (dbUserName_lbl == null) {
			dbUserName_lbl = new JLabel();
			dbUserName_lbl.setText("User Name:");
			dbUserName_lbl.setBounds(20, 160, 70, 20);
		}
		return dbUserName_lbl;
	}

	private JTextField getDbUserName() {
		if (dbUserName == null) {
			dbUserName = new JTextField();
			dbUserName.setToolTipText("Enter the Database user name.");
			dbUserName.setBounds(100, 160, 140, 20);
		}
		return dbUserName;
	}

	private JLabel getDbPassword_lbl() {
		if (dbPassword_lbl == null) {
			dbPassword_lbl = new JLabel();
			dbPassword_lbl.setText("Password:");
			dbPassword_lbl.setBounds(20, 190, 70, 20);
		}
		return dbPassword_lbl;
	}

	private JPasswordField getDbPassword() {
		if (dbPassword == null) {
			dbPassword = new JPasswordField();
			dbPassword.setToolTipText("Enter the Database user password.");
			dbPassword.setBounds(100, 190, 140, 20);
		}
		return dbPassword;
	}

	private JToolBar getToolbar() {
		if (toolbar == null) {
			toolbar = new JToolBar();
			toolbar.setBounds(0, 0, 330, 42);
			toolbar.add(getExitButton());
			toolbar.add(getClearButton());
			toolbar.add(getQuickLoadButton());
			toolbar.add(getQuickSaveButton());
			toolbar.add(getStartButton());
			toolbar.setRollover(true);
			toolbar.setFloatable(false);			
		}
		return toolbar;
	}

	private JButton getExitButton() {
		if (exitButton == null) {
			exitButton = new JButton();
			exitButton.setText("Exit MMDSS");
			exitButton.setIcon(new ImageIcon(getClass().getResource("/door_out.png")));
			exitButton.setFocusable(false);
			exitButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
			exitButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
			exitButton.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent event) {
					exitButtonMouseMouseClicked(event);
				}
			});
		}
		return exitButton;
	}

	private JButton getClearButton() {
		if (clearButton == null) {
			clearButton = new JButton();
			clearButton.setText("Clear Fields");
			clearButton.setIcon(new ImageIcon(getClass().getResource("/cross.png")));	
			clearButton.setFocusable(false);
			clearButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
			clearButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
			clearButton.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent event) {
					clearButtonMouseMouseClicked(event);
				}
			});
		}
		return clearButton;
	}
	
	private JButton getQuickLoadButton() {
		if (quickSaveButton == null) {
			quickSaveButton = new JButton();
			quickSaveButton.setText("Quick Save");
			quickSaveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/disk.png")));
			quickSaveButton.setFocusable(false);
			quickSaveButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
			quickSaveButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
			quickSaveButton.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent event) {
					quickSaveButtonMouseMouseClicked(event);
				}
			});
		}
		return quickSaveButton;
	}
	
	private JButton getQuickSaveButton() {
		if (quickLoadButton == null) {
			quickLoadButton = new JButton();
			quickLoadButton.setText("Quick Load");
			quickLoadButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/folder_page_white.png")));
			quickLoadButton.setFocusable(false);
			quickLoadButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
			quickLoadButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
			quickLoadButton.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent event) {
					quickLoadButtonMouseMouseClicked(event);
				}
			});
		}
		return quickLoadButton;
	}

	private JButton getStartButton() {
		if (startButton == null) {
			startButton = new JButton();
			startButton.setText("Start");
			startButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/database_connect.png")));			
			startButton.setFocusable(false);
			startButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
			startButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
			startButton.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent event) {
					startButtonMouseMouseClicked(event);
				}
			});
		}
		return startButton;
	}

	private void exitButtonMouseMouseClicked(MouseEvent event) {
		Operations.systemEnd();
	}

	private void clearButtonMouseMouseClicked(MouseEvent event) {
		dbType.setSelectedItem("");
		dbHost.setText("");
		dbName.setText("");
		dbUserName.setText("");
		dbPassword.setText("");
	}
	
	private void quickSaveButtonMouseMouseClicked(MouseEvent event) {
		params.setProperty("dbType", (String)(dbType.getSelectedItem()));
        params.setProperty("dbHost", dbHost.getText());
        params.setProperty("dbName", dbName.getText());
        params.setProperty("dbUserName", dbUserName.getText());
        params.setProperty("dbPassword", String.valueOf(dbPassword.getPassword()));

        String fileName = "QuickSaveParameters.xml";
        String coment = "Saved parameters for MMDSS.";
        
        try {
            FileOutputStream fos = new FileOutputStream(fileName);
            params.storeToXML(fos, coment);
            fos.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
	}

	private void quickLoadButtonMouseMouseClicked(MouseEvent event) {
		String fileName = "QuickSaveParameters.xml";
        
        File file = new File(fileName);
        if (file.exists()) {
            try {
                FileInputStream in = new FileInputStream(file);
                params.loadFromXML(in);
                in.close();
                
                dbType.setSelectedItem(params.getProperty("dbType"));
        		dbHost.setText(params.getProperty("dbHost"));
        		dbName.setText(params.getProperty("dbName"));
        		dbUserName.setText(params.getProperty("dbUserName"));
        		dbPassword.setText(params.getProperty("dbPassword"));
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }  
        }
	}


	private void startButtonMouseMouseClicked(MouseEvent event) {		
		// Type, Host, Name, User, Password
		String[] params = {
			dbType.getSelectedItem().toString(),
			dbHost.getText(),
			dbName.getText(),
			dbUserName.getText(),
			String.valueOf(dbPassword.getPassword())
		};
		Operations.startSchemaAnalysis(params);
	}
}
